package cn.medemede.servlet;

import cn.medemede.servlet.action.Action;
import cn.medemede.servlet.factory.ActionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "JServlet")
public class JServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/text;charset=GBK");
        String actionName = req.getParameter("actionName");
        PrintWriter out = resp.getWriter();
        System.out.println("Action=" + actionName);
        Action action;
        switch (actionName) {
            case "Log_check": {
                action = ActionFactory.getLogCheck();
                break;
            }
            case "Do_join": {
                action = ActionFactory.getDoJoin();
                break;
            }
            case "Do_change": {
                action = ActionFactory.getDoChenge();
                break;
            }
            case "clear": {
                action = ActionFactory.getClear();
                break;
            }
            case "studel":{
                action=ActionFactory.getStuDel();
                break;
            }
            default:
                throw new RuntimeException("请求失败！");
        }
        String forward = "";
        try {
            forward = action.execute(req, resp);
            System.out.println(forward);

            out.println(forward);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
